﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BlackHtml5.WebSite.Models.DAL
{
    public class GlobalData
    {
        
public struct __GetSysConfig_configs__:NFinal.IModel
		{
			/// <summary>
            /// 
            /// </summary>
			public System.Int64 id;
			/// <summary>
            /// 
            /// </summary>
			public System.String name;
			/// <summary>
            /// 
            /// </summary>
			public System.String info;
			/// <summary>
            /// 
            /// </summary>
			public System.String value;
						#region Nullable
			private bool __is_null__;
			public bool __Is_Null__
			{
				get {
					return __is_null__;
				}
				set
				{
					 __is_null__=value;
				}
			}
			public static implicit operator __GetSysConfig_configs__(string __value__)
			{
				var __value2__ = new __GetSysConfig_configs__();
				__value2__.__Is_Null__ = __value__==null;
				return __value2__;
			}
			public static  bool operator true(__GetSysConfig_configs__ __value__)
			{
				return !__value__.__is_null__;
			}
			public static bool operator false(__GetSysConfig_configs__ __value__)
			{
				return !__value__.__is_null__;
			}
			#endregion
			#region 写Json字符串
			public void WriteJson(System.IO.TextWriter tw)
			{
				tw.Write("{");

						tw.Write("\"id\":");
						tw.Write(id.ToString());
						tw.Write(",");
						tw.Write("\"name\":");
						if(name==null)
						{
							tw.Write("null");
						}
						else
						{
							tw.Write("\"");
							tw.Write(name);
							tw.Write("\"");
						}
						tw.Write(",");
						tw.Write("\"info\":");
						if(info==null)
						{
							tw.Write("null");
						}
						else
						{
							tw.Write("\"");
							tw.Write(info);
							tw.Write("\"");
						}
						tw.Write(",");
						tw.Write("\"value\":");
						if(value==null)
						{
							tw.Write("null");
						}
						else
						{
							tw.Write("\"");
							tw.Write(value);
							tw.Write("\"");
						}
							    tw.Write("}");
			}
			#endregion
		}
		public Dictionary<string,string> GetSysConfig(System.Data.IDbConnection conn)
        {
            
            Dictionary<string, string> global = (Dictionary<string, string>)System.Runtime.Caching.MemoryCache.Default.Get("db:sys_config");
            if (global == null)
            {
                global = new Dictionary<string, string>();
                var con=(System.Data.SQLite.SQLiteConnection)conn;
                
#region	var configs;选取所有记录
			var configs = new List<__GetSysConfig_configs__>();
			var __GetSysConfig_configs_command__ = new System.Data.SQLite.SQLiteCommand("select * from sys_config", con);
						
						var __GetSysConfig_configs_reader__= __GetSysConfig_configs_command__.ExecuteReader();
			if (__GetSysConfig_configs_reader__.HasRows)
			{
				while (__GetSysConfig_configs_reader__.Read())
				{
					var __GetSysConfig_configs_temp__ = new __GetSysConfig_configs__();
					
					__GetSysConfig_configs_temp__.id = __GetSysConfig_configs_reader__.GetInt64(0);
					__GetSysConfig_configs_temp__.name =__GetSysConfig_configs_reader__.IsDBNull(1)?null: __GetSysConfig_configs_reader__.GetString(1);
					__GetSysConfig_configs_temp__.info =__GetSysConfig_configs_reader__.IsDBNull(2)?null: __GetSysConfig_configs_reader__.GetString(2);
					__GetSysConfig_configs_temp__.value =__GetSysConfig_configs_reader__.IsDBNull(3)?null: __GetSysConfig_configs_reader__.GetString(3);
					configs.Add(__GetSysConfig_configs_temp__);
				}
			}
			__GetSysConfig_configs_reader__.Dispose();
			__GetSysConfig_configs_command__.Dispose();
			#endregion
			
                foreach (var c in configs)
                {
                    global.Add(c.name, c.value);
                }
                System.Runtime.Caching.MemoryCache.Default.Add("db:sys_config", global, DateTimeOffset.Now.AddHours(5));
            }
            return global;
        }
    }
}